package evote;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.*;
import java.util.HashMap;

public class Graph {
	
	private Vertex [] vertexes;
	private Edge[] edges;
	private int GraphID;
	private Permutation bestPermution;
	private HashMap<String, Vertex> graphHashToProb; // Hashmap of <MD5_hash_Value_On_Grpah,Calculated_Value(=Probability)>
	
	// Build graph from a given permutation
	Graph(Permutation perm){
		bestPermution = perm; //TODO: maybe we want to find the best permeation for easy calc'
		GenerateVertexs(perm.GetSize() / 2);
		
	}
	
	private void GenerateVertexs(int size){
		sourceVertex = new Vertex[size];
		targetVertex = new Vertex[size];
		for(int i = 0 ; i < size ; i++ ){
			sourceVertex[i] = new Vertex();
			targetVertex[i] = new Vertex();
		}
		//todo create all the statistics and such
	}
	
	private void GenerateEdges(int size){
		edges = new Edge[size];
		for (int i = 0 ; i < size ; i++){
			//edges[i] = new Edge(source, target, size);
		}
	}
	
	private void CalculateID(){
		for(int i = 0 ; i < edges.length ; i++)
			GraphID+= edges[i].GetID();
	}
	
	private String MD5(String graphStr){
	
		byte[] bytesOfMessage;
		
		try {
			bytesOfMessage = graphStr.getBytes("UTF-8");
			MessageDigest m = MessageDigest.getInstance("MD5");
			m.reset();
			byte[] digest = m.digest(bytesOfMessage);
			BigInteger bigInt = new BigInteger(1,digest);
			String hashtext = bigInt.toString(16);
			while(hashtext.length() < 32 ){ // zero Pad for 32 bytes - TODO: maybe we need less then 32 bytes.
				  hashtext = "0"+hashtext;
			}
			return hashtext;	
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		return null; // Should not get here.
	}
	
	private void addVertex(int leftId, int rightId){
		vertexes[leftId] = new Vertex(leftId, rightId);
	}
	
	private void addEdge(int sourceId, int targetId,int leftOrRight){ //left = 0 ,right = 1
		edges[sourceId+leftOrRight] = new Edge(sourceId,targetId,);//TODO: not closed on the edge repr.
	}
	//TODO: private double calcRecursion() {}
	//TODO: private String toString () {} // Graph representation to string.

}

